import qs from 'qs';
import { ApiUrl } from './env.js';
import { processParams } from '@/unit/common.js';
// import store from '@/store';
const ajax = (opt) => {
  const from = window.location.href.split('#')[0];
  try {
    const token = uni.getStorageSync('user_token');
    // if (token && store.state.userInfo.rank < 2 && window.location.href.split('#')[0].indexOf('pages/join/') === -1 && window.location.href.split('#')[0].indexOf('pages/purchaseShop/goodsDetail') === -1) {
    //   uni.reLaunch({
    //     url: '/pages/join/result'
    //   });
    //   return false;
    // }
    if (token || (!token && opt.noAuth)) {
      opt = opt || {};
      opt.url = opt.url || '';
      opt.data = processParams(opt.data) || null;
      const crypto = require('crypto');
      const hash = crypto.createHash('md5');
      opt.data.sign = hash.update(decodeURIComponent(qs.stringify(processParams(opt.data)))).digest('hex');
      opt.method = opt.method || 'POST';
      if (opt.method === 'POST') {
        opt.data = qs.stringify(processParams(opt.data));
      }
      opt.header = opt.header || {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Token': token
      };
      opt.ApiUrl = opt.apiUrl || ApiUrl;
      opt.success = opt.success || function () {};
      opt.fail = opt.fail || function () {};
      uni.request({
        url: opt.ApiUrl + opt.url,
        data: opt.data,
        method: opt.method,
        header: opt.header,
        responseType: opt.responseType ? 'arraybuffer' : 'text',
        dataType: 'json',
        success: function (res) {
          if (res.data.status === 2) {
           
          } else if (res.data.status === 1 && opt.opt && opt.opt.showErrorMsg) {
            uni.showToast({
              title: res.data.msg,
              icon: 'none'
            });
          } else if (res.data.status === 4) { // 加盟审核结果页
            uni.showToast({
              title: res.data.msg,
              icon: 'none',
              success: () => {
                setTimeout(() => {
                  uni.reLaunch({
                    url: '/pages/join/result'
                  });
                }, 2000);
              }
            });
          } else if (res.data.status === 6) { // 免费商户审核结果页
            uni.showToast({
              title: res.data.msg,
              icon: 'none',
              success: () => {
                setTimeout(() => {
                  uni.reLaunch({
                    url: '/pages/freeStore/result'
                  });
                }, 2000);
              }
            });
          } else {
            opt.success(res.data);
          }
        },
        fail: function () {
          opt.fail();
          uni.showToast({
            title: '请稍后重试'
          });
        }
      });
    } else {
      uni.showToast({
        title: '请先登录！',
        icon: 'none',
        success: () => {
          
        }
      });
    }
  } catch (e) {
    // error
    uni.showToast({
      title: '请先登录！' + JSON.stringify(e),
      icon: 'none',
      success: () => {
        
      }
    });
  }
};

export {
  ajax
}